﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using SalesManagement.Modules.List;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class BankForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string bankId)
        {
            BankEntity bankE = new BankEntity();
            BankService bankS = BankService.GetInstance();
            bankE.BankId = int.Parse(bankId);
            bankE = bankS.LoadByPrimaryKey(bankE);
            if (null == bankE)
                return;
            hdfBankId.Value = bankE.BankId;
            txtBankName.Text = bankE.BankName;
            txtDescription.Text = bankE.Description;
            ckbIsActive.Checked = bankE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            BankEntity bankE = new BankEntity();
            BankService bankS = BankService.GetInstance();
            if ("" != hdfBankId.Value.ToString())
            {
                string bankId = hdfBankId.Value.ToString();
                bankE.BankId = int.Parse(bankId);
                bankE = bankS.LoadByPrimaryKey(bankE);
                bankE.BankName = txtBankName.Text;
                bankE.Description = txtDescription.Text;
                bankE.IsActive = ckbIsActive.Checked;
                bankE.UpdatedDate = DateTime.Now;
                bankE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                bankS.Update(bankE);
            }
            else
            {
                bankE.BankName = txtBankName.Text;
                bankE.Description = txtDescription.Text;
                bankE.IsActive = ckbIsActive.Checked;
                bankE.CreatedDate = DateTime.Now;
                bankE.CreatedBy = FSSContext.Current.User.EmployeeId;
                bankE.UpdatedDate = DateTime.Now;
                bankE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                bankS.Insert(bankE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}